package com.demo.impl;

import com.demo.mapper.TeacherMapper;
import com.demo.pojo.Teacher;
import com.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public void saveTeacher(Teacher teacher) {
        teacherMapper.insert(teacher);
    }

    @Override
    public Teacher queryById(String id) {
        Teacher teacher = teacherMapper.selectByPrimaryKey(id);
        return teacher;
    }

    @Override
    public List<Teacher> queryByCondition(String sex, Integer age) {
        Example example = new Example(Teacher.class);
        Example.Criteria criteria = example.createCriteria();
        // 创建匹配条件, where sex = sex and age = age
        criteria.andEqualTo("sex", sex);
        criteria.andEqualTo("age", age);

        // 也可以用另外一种方式来查询
//        Teacher teacher = new Teacher();
//        teacher.setSex(sex);
//        teacher.setAge(age);
//        List<Teacher> teacherList = teacherMapper.select(teacher);

        List<Teacher> teacherList = teacherMapper.selectByExample(example);
        return teacherList;
    }

    @Override
    public void updateTeacher(Teacher teacher) {
        teacherMapper.updateByPrimaryKey(teacher);
    }

    @Override
    public void deleteTeacher(Teacher teacher) {
        teacherMapper.deleteByPrimaryKey(teacher.getId());
    }
}
